layui.use(['form','jquery','layer','table'],function () {
    var form = layui.form;
    var $ = layui.$;
    var layer = layui.layer;
    var table = layui.table;
    getSlideshowsByDisplayImgType();
    $(document).click(function () {
        parent.$('.tab').hide();
    });
    //返回按钮
    $('#goBack').click(function () {
        var title = $('#title').text();
        var iframe = parent.$('.iframe');
        $.each(iframe,function (i,item) {
            if(title == $(item).attr('_href')){
                parent.$('#beforeHref').val($(item).attr('_href'));
                parent.beforeHref();
                return;
            }
        });
    });

    form.on('submit(search)',function (obj) {
        getSlideshowsByDisplayImgType(obj.field);
        return false;
    })
    //收起按钮
    $('#packUp').click(function () {
        if($(this).text() == ' 收起'){
            $('.layui-card-body').slideUp(300,'linear');
            $('#packUp').text(' 展示').removeClass('layui-icon-down').addClass('layui-icon-up');
        }else if($(this).text() == ' 展示'){
            $('.layui-card-body').slideDown(300,'linear');
            $('#packUp').text(' 收起').removeClass('layui-icon-up').addClass('layui-icon-down');
        }
    })
    //刷新按钮
    $('#refresh').click(function () {
        location.reload()
    });
    //根据图片类型和显示状态查询轮播图
    function getSlideshowsByDisplayImgType(jsCarousel) {
        $.ajax({
            url: '/JsCarousel/getSlideshowsByDisplayImgType',
            data: jsCarousel,
            type: 'post',
            dataType: 'json',
            success: function (data) {
                slideshowsRender(data.data);
            }
        });
    }
    //将查询出来的数据进行table渲染
    function slideshowsRender(data){
        table.render({
            elem: '#slideshowsTable',
            title: '轮播图列表',
            data: data,
            limit: 10,
            width: 1630,
            toolbar: '#button_ids',
            page:{
                layout:['count','prev','page','next','limit','skip','refresh'],
                //limits:[3,6,9],
                first:"首页",
                last:"尾页",
            },
            cols: [[
                {type:'checkbox'},
                {title:'序号',align:'center',width:180,templet:function(data){
                        return data.LAY_INDEX;
                    }},
                {field:"imgTitle",title:"图片标题",align:'center',width:180},
                {field:"userAccount",title:"创建人",align:'center',width:180},
                {field:"",title:"图片",align:'center',templet:function(data){
                    return '<img src="'+ data.imgAddress+'" />';
                }},
                {field:"startDate",title:"开始时间",align:'center',width:180},
                {field:"expireDate",title:"到期时间",align:'center',width:180},
                {field:"remarks",title:"备注",align:'center',width:180},
                {field:'',title:"操作",align:'center',width:180,toolbar: '#operation'}
            ]]
        });
    }
    table.on('tool(slideshowsTable)',function (data) {
        if (data.event == 'updateTable') {
            layer.open({
                type: 2,
                title: '修改轮播图',
                content: '/advertising/slideshowManagement/slideshowList/update-slideshow.html',
                area: ['1150px', '550px'],
                offset: 'auto',
                shade: 0.4,
                success:function(layero,index) {
                    var slideshow = data.data;
                    var childHtml = layero.find('iframe').contents();
                    childHtml.find('#imgTitle').val(slideshow.imgTitle);
                    childHtml.find('#describe').val(slideshow.describe);
                    childHtml.find('#startDate').val(slideshow.startDate);
                    childHtml.find('#expireDate').val(slideshow.expireDate);
                    childHtml.find('#remarks').val(slideshow.remarks);
                    childHtml.find('#currentImage').attr('src',slideshow.imgAddress);
                    childHtml.find('#carouselId').val(slideshow.carouselId);
                    var display;
                    if(slideshow.display == 2){
                        display = '上线'
                    }else if(slideshow.display == 3){
                        display = '下线'
                    }
                    radio(childHtml,'input[name=displayName]',display);
                },
                end:function () {
                    getSlideshowsByDisplayImgType();
                }

            })
            //删除按钮
        }else if(data.event == 'deleteTable'){
            var layerIndex = layer.confirm('确定要删除轮播图"' + data.data.imgTitle + '"么?', {
                btn: ['确定','取消'],
                icon: 3,
                title: '提示'
            }, function(){
                delCurrentSlideshow(data.data.carouselId);
                layer.close(layerIndex);
            }, function(){
                layer.close(layerIndex);
            });
        }
    })
    //删除当前行订单
    function delCurrentSlideshow(carouselId) {
        $.ajax({
            url: '/JsCarousel/delCurrentSlideshow',
            data: {carouselId: carouselId},
            type: 'post',
            dataType: 'json',
            success:function (data) {
                layer.msg(data.data);
                getSlideshowsByDisplayImgType();
            }
        });
    }
    function radio(childHtml,elem,attributeValue) {
        childHtml.find(elem).each(function () {
            if($(this).val() == attributeValue){
                $(this).parent().children('div').children('div').each(function () {
                    if($(this).text() == attributeValue){
                        $(this).prev().click();
                    }
                })
            }
        })
    }

})